package com.ma.schoolproject.service.Imp;

import ch.qos.logback.core.joran.util.beans.BeanUtil;
import cn.dev33.satoken.secure.SaSecureUtil;
import cn.dev33.satoken.util.SaResult;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ma.schoolproject.config.SaTokenProperties;
import com.ma.schoolproject.entitys.User;
import com.ma.schoolproject.entitys.UserVO;
import com.ma.schoolproject.mapper.UserMapper;
import com.ma.schoolproject.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Transactional
public class UserServiceImp extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private SaTokenProperties saTokenProperties;

    @Override
    public SaResult selectUser(String id) {
        User user = userMapper.selectById(id);
        if (user != null) {
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(user, userVO);
            userVO.setStatus(user.getStatus() == 0 ? "正常" : "异常");
            return SaResult.data(userVO);
        }
        return SaResult.error("用户不存在");
    }

    @Override
    public SaResult updateUser(User user) {
        User user1 = userMapper.selectById(user.getId());
        if (user1 == null) {
            return SaResult.error("用户不存在");
        }
        if(user.getPassword()!=null&&!"".equals(user.getPassword().trim())){
            String s = SaSecureUtil.aesEncrypt(saTokenProperties.getVerifyKey(), user.getPassword());
            user.setPassword(s);
        }
        int i = userMapper.updateById(user);
        return i > 0 ? SaResult.ok("修改成功") : SaResult.error("更新失败");
    }

    @Override
    public SaResult saveUser(User user) {
        String s = SaSecureUtil.aesEncrypt(saTokenProperties.getVerifyKey(), user.getPassword());
        user.setPassword(s);
        int i = userMapper.insert(user);
        return i > 0 ? SaResult.ok("保存成功") : SaResult.error("保存失败");
    }

    //eq()（等于） ne()（不等于） gt()（大于） ge()（大于等于） lt()（小于）le()（小于等于）
    @Override
    public SaResult pageUser(Integer page, Integer size, Map<String, Object> searchMap) {
        Page<User> userPage=new Page<>(page,size);
        LambdaQueryWrapper<User> wrapper=new LambdaQueryWrapper<>();
        // 查询提交时间在某个范围之间的用户
        if (searchMap.get("startTime") != null && !searchMap.get("startTime").toString().isEmpty()) {
            wrapper.ge(User::getSubmitTime, searchMap.get("startTime"));  // submit_time >= startTime
        }

        if (searchMap.get("endTime") != null && !searchMap.get("endTime").toString().isEmpty()) {
            wrapper.le(User::getSubmitTime, searchMap.get("endTime"));    // submit_time <= endTime
        }
        Page<User> userPage1 = userMapper.selectPage(userPage, wrapper);
        // 转换为 UserVO Page
        Page<UserVO> userVOPage = new Page<>(userPage1.getCurrent(), userPage1.getSize(), userPage1.getTotal());
        List<UserVO> userVOList = userPage1.getRecords().stream().map(user -> {
            UserVO userVO = new UserVO();
            BeanUtils.copyProperties(user, userVO);
            userVO.setStatus(user.getStatus() == 0 ? "正常" : "异常");
            return userVO;
        }).collect(Collectors.toList());

        userVOPage.setRecords(userVOList);
        return SaResult.data(userVOPage);
    }
}
